package tgc.edu.ljc.custom;

import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;
/**
 * 创建等比缩略图
 * @author ljc
 *
 */
public class CreateShortImage {
    public  static  String DDIST="d:/media/shortImage/";  //缩略图文件夹
    public  static  String DSRC="d:/media/";   //源图片文件夹
    
    /**
     * 创建图片缩略图(等比缩放)
     * @param src 源图文件名
     * @param dist 缩略图文件名
     * @param width 缩略图宽
     * @param height 缩略图高
     */
    /*public static void createThumbnail(String src,String dist,float width,float height){
        try {
            File srcFile = new File(src);
            if(!srcFile.exists()){
                System.out.println("文件不存在");
                return;
            }
            BufferedImage image = ImageIO.read(srcFile);
            //获得缩放比例
            double ratio = 1.0;
            //判断如果高、宽都不大于设定值，则不处理
            if(image.getHeight() > height || image.getWidth() > width){
                if( image.getHeight() > image.getWidth()){
                    ratio = height / image.getHeight();
                }else{
                    ratio = width / image.getWidth();
                }
            }
            //计算新的图面宽度和高度
            int newWidth = (int)(image.getWidth() * ratio);
            int newHeight = (int)(image.getHeight() * ratio);
            BufferedImage bfiImage = new BufferedImage(newWidth, newHeight,BufferedImage.TYPE_INT_BGR);
            bfiImage.getGraphics().drawImage(
                    image.getScaledInstance(newWidth, newHeight,Image.SCALE_SMOOTH),0,0,null);
            FileOutputStream os = new FileOutputStream(dist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
            encoder.encode(bfiImage);
            os.close();
            System.out.println("创建缩略图成功");
        } catch (Exception e) {
            System.out.println("创建缩略图发生异常"+e.getMessage());
        }
    }*/
    
    /***
     * 等比缩放图片
     * @param src 源文件
     * @param desc 目标文件
     * @param width 目标图片宽
     * @param height 目标图片高
     */
    public static void createThumbnail(String src,String desc,int width,int height) {
    	try {
			Thumbnails.of(src).size(width, height).keepAspectRatio(true).outputFormat("png").toFile(desc);
    		//Thumbnails.of(src).scale(0.5).toFile(desc+".png");
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    public static void createThumbnail(String src,String desc,double scale) {
    	try {
			//Thumbnails.of(src).size(width, height).keepAspectRatio(true).outputFormat("png").toFile(desc);
    		Thumbnails.of(src).scale(scale).outputFormat("jpg").toFile(desc);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
	/*
	 * public static void main(String[] args) { createThumbnail("d:/aaa/p3.JPG",
	 * "d:/aaa/p3(1)",0.5); System.out.println("over");
	 * //createThumbnail(DSRC+"2.png", DDIST+"b.png", 100, 100);
	 * //createThumbnail(DSRC+"3.jpg", DDIST+"c.jpg", 100, 100); }
	 */
}
